package com.sugon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.DevAmPlanItemDao;
import com.sugon.entity.DevAmPlanItemEntity;
import com.sugon.modules.device.model.DevMaintenPlanItemVO;
import com.sugon.modules.device.model.vo.DevMaintenancePlanDeviceVO;
import com.sugon.modules.device.model.vo.DevMaintenancePlanItemVO;
import com.sugon.service.DevAmPlanItemService;
import com.sugon.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备维护计划项信息Service实现类
 *
 * @author sugon
 * @date 2020-07-26 20:00:49
 */
@Service("devAmMaintenancePlanItemService")
public class DevAmPlanItemServiceImpl implements DevAmPlanItemService {

    @Autowired
    private DevAmPlanItemDao devPlanMainItemDao;

    @Override
    public List<DevAmPlanItemEntity> getCurrentYearlyPlanItemListByDevId(String devId) {
        // 获取本年度
        Integer yearly = cn.hutool.core.date.DateUtil.thisYear();
        List<DevAmPlanItemEntity> planItemList = devPlanMainItemDao.getYearlyPlanItemByDevId(devId, yearly);
        if (planItemList == null) {
            planItemList = new ArrayList<>();
        }
        return planItemList;
    }

    /**
     * queryDevList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevMaintenancePlanDeviceVO> queryDevList(Map<String, Object> map) {
        // 查询当前频率下的设备维护项列表
        List<DevMaintenancePlanDeviceVO> maintenanceItemList = devPlanMainItemDao.queryDevMaintenancePlanItemDetails(map);
        List<DevMaintenancePlanDeviceVO> itemList = CollectionUtil.newArrayList();
        if (CollectionUtil.isNotEmpty(maintenanceItemList)) {
            for (DevMaintenancePlanDeviceVO maintenanceItem : maintenanceItemList) {
                List<DevMaintenancePlanItemVO> planItems = maintenanceItem.getItems();
                if (CollectionUtil.isEmpty(planItems)) {
                    continue;
                }
                for (DevMaintenancePlanItemVO planItem : planItems) {
                    DevMaintenancePlanDeviceVO newMaintenanceItem = new DevMaintenancePlanDeviceVO();
                    BeanUtil.copyProperties(maintenanceItem, newMaintenanceItem);
                    BeanUtil.copyProperties(planItem, newMaintenanceItem);
                    itemList.add(newMaintenanceItem);
                }
            }
        }
        return itemList;
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevMaintenPlanItemVO> queryList(Map<String, Object> map) {
        return devPlanMainItemDao.queryList2(map);
    }

    @Override
    public List<DevMaintenPlanItemVO> queryByPlanId(String planId, String devId) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("planId", planId);
        if (StrUtil.isNotEmpty(devId)) {
            queryParam.put("devId", devId);
        }
        return queryList(queryParam);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devPlanMainItemDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param devPlanMainItem 实体
     * @return
     */
    @Override
    public int save(DevAmPlanItemEntity devPlanMainItem) {
        devPlanMainItem.setId(RandomUtil.randomString(32));
        devPlanMainItem.setGmtCreate(DateUtil.now());
        devPlanMainItem.setIsEnabled(1);
        return devPlanMainItemDao.save(devPlanMainItem);
    }

    /**
     * update
     *
     * @param devPlanMainItem 实体
     * @return
     */
    @Override
    public int update(DevAmPlanItemEntity devPlanMainItem) {
        devPlanMainItem.setGmtModified(cn.hutool.core.date.DateUtil.date());
        return devPlanMainItemDao.update(devPlanMainItem);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devPlanMainItemDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devPlanMainItemDao.deleteBatch(ids);
    }

    /**
     * isExists
     *
     * @param planId     维护计划ID
     * @param devId      设备ID
     * @param mainBaseId 维系项ID
     * @return
     */
    @Override
    public boolean isExists(String planId, String devId, String mainBaseId) {
        Map<String, Object> param = new HashMap<>(2);
        if (StrUtil.isNotEmpty(planId)) {
            param.put("planId", planId);
        }
        if (StrUtil.isNotEmpty(devId)) {
            param.put("devId", devId);
        }
        if (StrUtil.isNotEmpty(mainBaseId)) {
            param.put("mainBaseId", mainBaseId);
        }
        return devPlanMainItemDao.queryTotal(param) > 0;
    }
}
